import {getUrlParam} from "@/static/js/filter.js"
export default {
	data () {
		return {
			appConstitutionEntityVo:[],  //从缓存中获取
			submitAppConstitutionEntityVo:[], //点击提交
			calculatePoints: {},
			defaultArr:['平和质','气虚质','阳虚质','阴虚质','痰湿质','湿热质','血瘀质','气郁质','特禀质'],
			id:'',
			backId: '',
			calculateEvent: '',
			healthName:'',
			result:{},
			type: '',
			currentIndex: 9,
			constitution:[],
			healthInformation: {}, //从缓存中获取健康管理师信息
			showModal:false,  //弹框
			showContent:'',
			status: '',
		}
	},
	created() {
		this.calculateEvent = localStorage.questionData;
		this.healthInformation = JSON.parse(localStorage.userInfo);
		if(getUrlParam("id")){
			this.id = getUrlParam("id");
			this.status = getUrlParam('status');
			this.$api.questionnaireInfoId(this.id).then((res) => {
				this.$set(this,'result',res.data.result);    
				this.$set(this.calculatePoints,'appPhysiqueScoreEntitiesvo',res.data.result.AppPhysiqueScoreEntityVo);
				this.$set(this.calculatePoints,'userName',res.data.result.appConstitution.userName)
				this.$set(this.calculatePoints,'createdDate',res.data.result.appConstitution.createdDate)
				this.$set(this.calculatePoints,'healthName',res.data.result.appConstitution.healthName);
				this.$set(this.calculatePoints,'signDate',res.data.result.appConstitution.signDate);
				this.$set(this.calculatePoints,'type',res.data.result.AppPhysiqueScoreEntityVo.type);
				this.$set(this,'constitution',res.data.result.appConstitution && res.data.result.appConstitution.constitution && JSON.parse(res.data.result.appConstitution.constitution) || '');
			})
		}
		this.appConstitutionEntityVo = localStorage.getItem('questionData') && JSON.parse(localStorage.getItem('questionData')).map((a) => {
			const bresult = a.data.map((b) => {
				var cresult = []
				cresult = b.options.filter((c) => {
					c.label = a.id
					c.healthOption = "string"
					c.healthParame =  0
					c.orde =  0
					return c.checked == true
				})
				delete cresult[0].title;
				delete cresult[0].checked;
				return cresult[0]
			})
			return bresult
		})
		var obj = []
		this.appConstitutionEntityVo && this.appConstitutionEntityVo.map((item,index) => {
			obj = obj.concat(item)
		});
		this.submitAppConstitutionEntityVo = {
			appConstitutionQuestionnaireEntities: obj,
			userId: Number(this.healthInformation.userId),
			type: 1,
			mark: 1,
			id: this.id ? Number(this.id) : '',
			healthId: Number(this.healthInformation.healthId),
		}

	},
	 methods: {
		// 点击tab返回到上一页指定的位置
		back(e) {
			this.currentIndex = e;
			if(this.backId) {
				this.id = this.backId;
				uni.navigateTo({
					url:'/pages/monitor/tcm/addquestionnaire?current='+e+'&id='+this.id+'&status='+this.status
				});
			} else if(this.id) {
				uni.navigateTo({
					url:'/pages/monitor/tcm/addquestionnaire?current='+e+'&id='+this.id+'&status='+this.status
				})
			} else {
				uni.navigateTo({
					url:'/pages/monitor/tcm/addquestionnaire?current='+e
				})
			}
			
		},
		// 点击计算转化分
		calculate() {
			this.submitAppConstitutionEntityVo.status = 3;
			this.$api.questionnaireSave(this.submitAppConstitutionEntityVo).then((res) => {
				if(res.data.returnCode == '200') {
					this.calculatePoints = res.data.result;  //计算转化分
					this.backId = res.data.result && res.data.result.id;   //列表编号
				}
			})             
		},
		// 截取时间
		shijiantime(times){
			var timearr = times.replace(" ", ":").replace(/\:/g, "-").split("-");
			var timestr = ""+timearr[0]+"-" + timearr[1] + "-" + timearr[2]
			return timestr
		},
		// 提示没有数据可以提交计算
		tips() {
			uni.showToast({title:'返回中医体质问卷添加新的问卷吧~',icon:'none'})
		},
		// 保存
		preserve(e) {
			this.submitAppConstitutionEntityVo.status = e;
			this.submitAppConstitutionEntityVo.id = this.backId ? this.backId : Number(this.id);
			if(e == 1) {
				this.$api.questionnaireSave(this.submitAppConstitutionEntityVo).then((res) => {
					if(res.data.returnCode == '200') {
						localStorage.removeItem('questionData');
						this.calculateEvent = '';
						uni.showToast({
							title: '保存成功',
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateTo({
								url:'/pages/monitor/tcm/list'
							})
						},1000)
					}
					
				})
			} else {
				this.showModal = true;
				this.showContent = '确认信息无误，存档后不可修改。';
			}
			
		},
		// 确认存档
		Modalconfirm() {
			this.submitAppConstitutionEntityVo.status = 2;
			this.$api.questionnaireSave(this.submitAppConstitutionEntityVo).then((res) => {
				if(res.data.returnCode == '200') {
					localStorage.removeItem('questionData');
					this.calculateEvent = '';
					uni.showToast({
						title: '存档成功',
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateTo({
							url:'/pages/monitor/tcm/list'
						})
					})
				}
				
			})
		},
		
		// 望闻切详情
		medical() {
			const id = this.result && this.result.appConstitutionEntityVo.id;
			uni.navigateTo({
				url:'/pages/monitor/tcm/tcmInfo?id='+id
			})
		}
	}
}